﻿using Models;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedisHelper.Search
{
    public class Searcing
    {
        /// <summary>
        /// 获取全部关键字集合
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<HashEntry> GetSearchList()
        {
            using (RedisHelper rh = new RedisHelper())
            {
                var list = rh.GetDatabase(7).HashGetAll(StaticConfigRidesName.SearchKeywords_Hash());
                return list.ToList();
            }
        }

        /// <summary>
        /// 添加关键字
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public bool InsertSearch(string text)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                var database = rh.GetDatabase(7);
                var ret = database.HashGet(StaticConfigRidesName.SearchKeywords_Hash(), text);
                if (ret.IsNull)
                    return database.HashSet(StaticConfigRidesName.SearchKeywords_Hash(), text, 0);
                else
                {
                    var count = Convert.ToInt32(ret) + 1;
                    return database.HashSet(StaticConfigRidesName.SearchKeywords_Hash(), text, count);
                }
            }
        }
    }
}
